package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.ViewHrJobRankSaveCmd;
import com.jackrain.nea.fc.cp.mapper.ViewHrJobRankMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.security.Security4Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * 职等006
 *
 * @author wuwenxue
 * @date 2017/12/15
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "cp")
public class ViewHrJobRankCmdImpl extends CommandAdapter implements ViewHrJobRankSaveCmd {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ViewHrJobRankMapper viewHrJobRankMapper = ApplicationContextHandle.getBean(ViewHrJobRankMapper.class);
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),
                "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewHrJobRankCmdImpl--param-->{}",param);

        if (null == param || param.size() == 0) {
            throw new NDSException("参数格式错误！");
        }

        if ((!param.containsKey("data")) || param.getJSONArray("data").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        JSONArray jobRankArray = param.getJSONArray("data");
        User user = Security4Utils.getUser("INTER99999");
        for (int i = 0; i < jobRankArray.size(); i++) {
            JSONObject jobRankJson = jobRankArray.getJSONObject(i);
            log.info("postJson：" + jobRankJson.toJSONString());

            jobRankJson.put("MODIFIERNAME", user.getName());
            jobRankJson.put("MODIFIERENAME", user.getEname());
            jobRankJson.put("MODIFIERID", Integer.valueOf(user.getId()).longValue());
            //判断新增或修改
            String hr006 = jobRankJson.getString("HR006_ID");
            int isHr006 = viewHrJobRankMapper.selForHR006id(hr006);
            if (isHr006 > 0) {
                //执行修改操作
                int isUpdate = viewHrJobRankMapper.updateJobRank(jobRankJson);
                if (isUpdate <= 0) {
                    throw new NDSException("职等更新失败！");
                } else {
                    vh.put("code", 0);
                    vh.put("message", Resources.getMessage("职等更新成功！"));
                }

            } else {
                //执行新增操作
                jobRankJson.put("ID", Tools.getSequence("CP_C_JOBRANK"));
                jobRankJson.put("AD_CLIENT_ID", Integer.valueOf(user.getClientId()).longValue());
                jobRankJson.put("AD_ORG_ID", Integer.valueOf(user.getOrgId()).longValue());
                jobRankJson.put("CREATIONDATE", jobRankJson.getString("MODIFIEDDATE"));
                jobRankJson.put("OWNERID", Integer.valueOf(user.getId()).longValue());
                jobRankJson.put("OWNERNAME", user.getName());
                jobRankJson.put("OWNERENAME", user.getEname());

                int isInsert = viewHrJobRankMapper.insertJobRank(jobRankJson);
                if (isInsert <= 0) {
                    throw new NDSException("职等新增失败！");
                } else {
                    vh.put("code", 0);
                    vh.put("message", Resources.getMessage("职等新增成功！"));
                }
            }
        }
        return vh;
    }


}
